package com.android.djlmapp.tools.other;

import android.util.Log;

import java.security.MessageDigest;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @name djlm
 * @class name：com.android.djlmapp.utils
 * @class describe: 文本工具类
 * @anthor wangziheng
 * @time 2017/5/8 13:47
 * @change
 * @chang time
 * @class describe
 */
public class TextUtil {

    /**
     * 字节数组转换为字符串
     * 卡尔
     *
     * @param src
     * @return
     */
    public static String bytesToASCString(byte[] src) {
        int len = src.length;

        if (src == null || len <= 0) {
            return null;
        }
        char[] result = new char[len];
        for (int i = 0; i < len; i++) {
            result[i] = (char) src[i];
        }

        return String.valueOf(result);
    }

    /**
     * 字节数组转字符串
     * 恒鸿达
     *
     * @param bcd
     * @return
     */
    public static String bytesToHexStr(byte[] bcd) {
        StringBuffer s = new StringBuffer(bcd.length * 2);

        for (int i = 0; i < bcd.length; i++) {
            boolean[] bcdLookup = new boolean[0];
            s.append(bcdLookup[(bcd[i] >>> 4) & 0x0f]);
            s.append(bcdLookup[bcd[i] & 0x0f]);
        }

        return s.toString();
    }

    /**
     * 字符串加密
     *
     * @param string
     * @return
     */
    public static String encode(String string) {
        try {
            byte[] hash = MessageDigest.getInstance("MD5").digest(string.getBytes("UTF-8"));
            StringBuilder hex = new StringBuilder(hash.length * 2);
            for (byte b : hash) {
                if ((b & 0xFF) < 0x10) {
                    hex.append("0");
                }
                hex.append(Integer.toHexString(b & 0xFF));
            }

            return hex.toString();
        } catch (Exception e) {
            Log.e("loginUtils", "encode---err: " + e);
            return "encode---err";
        }
    }

    /**
     * 用指定字符分隔格式化字符串
     * <br/>（如电话号为1391235678 指定startIndex为3，step为4，separator为'-'经过此处理后的结果为 <br/> 139-1234-5678）
     *
     * @param source     需要分隔的字符串
     * @param startIndex 开始分隔的索引号
     * @param step       步长
     * @param separator  指定的分隔符
     * @return 返回分隔格式化处理后的结果字符串
     */
    public static String separateString(String source, int startIndex, int step, char separator) {
        int times = 0;
        StringBuilder tmpBuilder = new StringBuilder(source);
        for (int i = 0; i < tmpBuilder.length(); i++) {
            if (i == startIndex + step * times + times) {//if(i == 3 || i == 8){
                if (separator != tmpBuilder.charAt(i)) {
                    tmpBuilder.insert(i, separator);
                }
                times++;
            } else {
                if (separator == tmpBuilder.charAt(i)) {
                    tmpBuilder.deleteCharAt(i);
                    i = -1;
                    times = 0;
                }
            }
        }
        return tmpBuilder.toString();
    }

    public static boolean isNumeric(String str) {
        Pattern pattern = Pattern.compile("[0-9]*");
        Matcher isNum = pattern.matcher(str);
        if (!isNum.matches()) {
            return false;
        }
        return true;
    }
}
